package simple.cashflow.service.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import simple.cashflow.model.Proyek;
import simple.cashflow.service.ProyekService;

@Service("proyekService")
public class ProyekServiceImpl implements ProyekService {
	
	@Autowired
	private SessionFactory sessionFactory;
	private String hqlCariSemua = "from Proyek";
	private String hqlCariDariKode = "from Proyek p where p.kodeProyek =:kode";

	@Override
	public void simpanProyek(Proyek proyek) {
		sessionFactory.getCurrentSession().saveOrUpdate(proyek);
	}

	@Override
	public void hapusProyek(Proyek proyek) {
		sessionFactory.getCurrentSession().delete(proyek);
	}

	@Override
	public Proyek cariProyekDariKode(String kodeProyek) {
		return (Proyek) sessionFactory.getCurrentSession().createQuery(hqlCariDariKode)
					.setParameter("kode", kodeProyek).uniqueResult();
	}

	@Override
	public List<Proyek> cariSemuaProyek() {
		return sessionFactory.getCurrentSession().createQuery(hqlCariSemua).list();
	}

}
